package se.lnu.axez;

import android.os.SystemClock;

public final class TouchEventTracker {
	public static final int EVENT_TYPE_CLICK = 1;
	public static final int EVENT_TYPE_SWIPE = 2;
	
	private static final long CLICK_TIMEOUT = 2000000L;
	private static final float CLICK_RANGE = 20f;

	public static TouchEventTracker obtain() {
		return new TouchEventTracker();
	}
	
	private float startX;
	private float startY;
	private long startTime;
	private int eventType;
	
	private TouchEventTracker() {
	}
	
	public void startEvent(float x, float y) {
		this.startX = x;
		this.startY = y;
		this.startTime = SystemClock.uptimeMillis();
		this.eventType = EVENT_TYPE_CLICK;
	}
	
	public void continueEvent(float x, float y) {
		if (Math.abs(this.startX - x) > CLICK_RANGE
				|| Math.abs(this.startY - y) > CLICK_RANGE
				|| SystemClock.uptimeMillis() - this.eventType > CLICK_TIMEOUT) {
			this.eventType = EVENT_TYPE_SWIPE;
		}
	}
	
	public void endEvent(float x, float y) {
		if (SystemClock.uptimeMillis() - this.startTime > CLICK_TIMEOUT) {
			this.eventType = EVENT_TYPE_SWIPE;
		}
	}

	public int getEventType() {
		return this.eventType;
	}
	
	public void recycle() {}
}
